設置 FireBase Authentication
Step 1 點選 Tool 會看到 FireBase
![](https://i.imgur.com/j5yd94i.png)
Step 2 點選 Authentication
![](https://i.imgur.com/SdNOX2l.png)
Step 3 需要去連接 FireBase帳號
![](https://i.imgur.com/HXgJCET.png)
Step 4 建立一個 新的 FireBase 專案 或 選擇舊的 FireBase 專案
![](https://i.imgur.com/feAGOqg.png)
Step 5 import FireBase Authentication 套件
![](https://i.imgur.com/OGzqzFN.png)
![](https://i.imgur.com/zxNBadu.png)
功能
登入
val auth = FirebaseAuth.getInstance()
auth.signInWithEmailAndPassword(acount, password).addOnCompleteListener(this, OnCompleteListener { task ->
if(task.isSuccessful) {
Toast.makeText(this, "Successfully Logged In", Toast.LENGTH_LONG).show()
}
else {
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show()
}
})
登出
FirebaseAuth.getInstance().signOut()
註冊
val auth = FirebaseAuth.getInstance()
auth.createUserWithEmailAndPassword(acount, password).addOnCompleteListener(this, OnCompleteListener{ task ->
if(task.isSuccessful){
Toast.makeText(this, "Registration Successed", Toast.LENGTH_LONG).show()
}else {
// If sign in fails, display a message to the user.
Log.d("TAG", "createUserWithEmail:failure", task.exception)
Toast.makeText(this, "Registration Failed", Toast.LENGTH_LONG).show()
}
})
重設密碼
val auth = FirebaseAuth.getInstance()
auth.sendPasswordResetEmail(email)
.addOnCompleteListener(this, OnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(this, "Reset link sent to your email", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "Unable to send reset mail", Toast.LENGTH_LONG).show()
}
})
FireBase 要記得設定認證選項
![](https://i.imgur.com/Oa1XR5K.png)
在FireBase 可以看到 使用者資料
![](https://i.imgur.com/4yf23pA.png)